A break method is used to exit a code. A break method is used in switch statement, and also, it can be used to
exit a while, for, or do loop. For example, the break statement in the following loop terminates the loop when the end of
input is reached.
while (true)
{
String input = JOptionPane.showInputDialog("Enter value, Cancel to quit");
if(input == null) // leave loop in the middle
**break**;
double x = Double.parseDouble(input);
data.add(x)
}
In java, there is a second form of the break statement that is used to break out of a nested statement. The statement " break label; " immediately jumps to the end of the statment that is tagged with a label. Any statement (including if and block
statements) can be tagged with a lavel---the syntax is
label: statement
The labeled break statement was invented to break out of a set of nested loops.
outerloop:
while(outer loop condition)
{ **. . .**
while(inner loop condition)
{ **. . .**
if(something really bad happened)
break outerloop;
}
}
//jumps here if something really bad happened//
exit a while, for, or do loop. For example, the break statement in the following loop terminates the loop when the end of
input is reached.
while (true) { String input = JOptionPane.showInputDialog("Enter value, Cancel to quit"); if(input == null) // leave loop in the middle **break**; double x = Double.parseDouble(input); data.add(x) }In java, there is a second form of the break statement that is used to break out of a nested statement. The statement" break label; " immediately jumps to the end of the statment that is tagged with a label. Any statement (including if and block
statements) can be tagged with a lavel---the syntax is
label: statement
The labeled break statement was invented to break out of a set of nested loops.
outerloop: while(outer loop condition) { **. . .** while(inner loop condition) { **. . .** if(something really bad happened) break outerloop; } } //jumps here if something really bad happened//